interface Listener {
    inPage?: boolean;
    once?: boolean;
    handler: (newVal: any, oldVal: any) => void;
    executed?: boolean;
    pageUrl?: string;
}
//3333333
interface Page {
    route: string;
}

interface SetQue {
    [key: string]: any;
}

// 假设的 getCurrentPages 函数实现
function getCurrentPages(): Page[] {
    return [{ route: 'currentPage' }];
}

export default class Store {
    private _store: Map<string, any> = new Map();
    private listenerList: Map<string, Listener[]> = new Map();

    constructor(parameters: SetQue) {
        this.store = parameters;
    }

    get store(): Map<string, any> {
        return this._store;
    }

    set store(obj: SetQue) {
        this.set(obj);
    }

    get(key: string): any {
        return this._store.get(key);
    }

    has(key: string): boolean {
        return this._store.has(key);
    }

    delete(key: string): boolean {
        return this._store.delete(key);
    }

    set(obj: string | SetQue, val?: any): void {
        if (typeof obj === "string") {
            this.triggerListener(obj, val, this._store.get(obj));
            this._store.set(obj, val);
        } else {
            Object.entries(obj).forEach(([key, value]) => {
                this.triggerListener(key, value, this._store.get(key));
                this._store.set(key, value);
            });
        }
    }

    on(key: string, listener: Listener | ((newVal: any, oldVal: any) => void)): void {
        const listenerConfig: Listener = typeof listener === "function" ? { handler: listener } : listener;
        listenerConfig.pageUrl = this.getPageUrl();
        this.setListener(key, listenerConfig);
    }

    setListener(key: string, listener: Listener): void {
        const listeners = this.listenerList.get(key) || [];
        listeners.push(listener);
        this.listenerList.set(key, listeners);
    }

    hasListener(key: string): boolean {
        return this.listenerList.has(key);
    }

    getCurrPage(): Page {
        const pageList: Page[] = getCurrentPages();
        return pageList[pageList.length - 1] || { route: '' };
    }

    getPageUrl(): string {
        return this.getCurrPage().route;
    }

    triggerListener(key: string, newVal: any, oldVal: any): void {
        const currPageUrl = this.getPageUrl();
        const listeners = this.listenerList.get(key) || [];
        listeners.forEach(listener => {
            if ((listener.inPage && currPageUrl === listener.pageUrl) || !listener.inPage) {
                if (!listener.once || !listener.executed) {
                    listener.handler(newVal, oldVal);
                    listener.executed = true;
                }
            }
        });
    }

    createProxy(store: any): any {
        Object.keys(store).forEach(key => {
            if (typeof store[key] === 'object') {
                store[key] = this.proxyStore(store[key]);
            }
        });
        return this.proxyStore(store);
    }

    proxyStore(store: any): any {
        return new Proxy(store, {
            get: (target, prop) => Reflect.get(target, prop),
            set: () => {
                console.warn('Warning: 请使用set方法操作store');
                return true;
            }
        });
    }
}
